(function() {

    var
    $u = juliet.using({
        validationResult : 'juliet.validation.validationResult',
        validator : 'juliet.validation.validator'
    }),
    vr = $u('validationResult');

    /**
     * Length validator
     */
    var LengthClass = function() {

        this.errorType = null;

        this.validate = function() {
            var
            value = this.chain.fieldValueHelper.fieldValue, o = this.metadata.options,
            isValueValid = true;

            if ((typeof value == 'undefined') || (o.min && (value.length < o.min))) {
                isValueValid = false;
                this.errorType = 'min';
            } else if (o.max && (value.length > o.max)) {
                isValueValid = false;
                this.errorType = 'max';
            }

            this.result = (!isValueValid ? vr.FAILED : vr.SUCCESS);
        };

        this.getError = function() {
            var tempErrorText = this.errorText;

            if (typeof this.errorText == 'object') {
                this.errorText = this.errorText[this.errorType];
            }
            
            var currentErrorText = this.base.getError.call(this, 'length', this.metadata.options);
            this.errorText = tempErrorText;
            return currentErrorText;
        };

    };

    $u('validator').addValidator('Length', LengthClass);

})();
